package cs2340.g21.wms;

import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class NoteAdapter extends ArrayAdapter<Note>{
    
    private static final String LOG_TAG = "debugger";
    Context context;
    int layoutResourceId;
    Note[] notes = null;
    
    public NoteAdapter(Context context, int layoutResourceId, Note[] notes){
        super(context, layoutResourceId, notes);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.notes = notes;
    }
    
    /**
     * This gets called once per element in notes and defines a row of the list
     * @param position current index in the array
     * @param convertView 
     * @param parent
     * @return the row with proper info filled out
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View row = convertView;
        NoteHolder holder = null;
        
        if(row == null){
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new NoteHolder();
            holder.email = (TextView)row.findViewById(R.id.noteName); // need default note user name
            holder.date = (TextView)row.findViewById(R.id.noteDate); // need default note date
            row.setTag(holder);
        }else{
            holder = (NoteHolder)row.getTag();
        }
            //Log.i(LOG_TAG, "making " + position);
            Note note = notes[position];
            holder.email.setText(note.getOwner().getEmail());
            int month = note.getDate().get(Calendar.MONTH) + 1;
            holder.date.setText(note.getDate().get(Calendar.DAY_OF_MONTH)+ "/" + month);
            //Log.i(LOG_TAG, "lol" +item.getDate().get(Calendar.DAY_OF_MONTH)+ "/" + item.getDate().get(Calendar.MONTH));

        return row;
        
    }
    
    /**
     * Static class to hold each note's relevant info (email = email of owner of note, not recipient)
     */
    static class NoteHolder{
        TextView date;
        TextView email;
    }
    
    

}
